#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteAnswerProvider : CY.GFive.Core.Providers.DALProvider.IVoteAnswerProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteAnswer = "SELECT [Id], [VoteAnswerConten],[VoteItemID] FROM [VoteAnswer] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteAnswer = "INSERT INTO [VoteAnswer]([VoteAnswerConten],[VoteItemID]) values(@VoteAnswerConten,@VoteItemID);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteAnswer = "UPDATE [VoteAnswer] SET [VoteAnswerConten] = @VoteAnswerConten,[VoteItemID] = @VoteItemID WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteAnswer = "DELETE [VoteAnswer] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteAnswer = "SELECT [Id], [VoteAnswerConten],[VoteItemID] FROM [VoteAnswer]";
		#endregion

		#region IVoteAnswerProvider Members
		public Core.Business.VoteAnswer Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteAnswer);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteAnswer voteAnswer = new Core.Business.VoteAnswer();

				if (!reader.IsDBNull(0)) voteAnswer.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteAnswer.VoteAnswerConten = reader.GetString(1);
				if (!reader.IsDBNull(2)) voteAnswer.VoteItemID = reader.GetInt32(2);

				return voteAnswer;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteAnswer voteAnswer)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteAnswerConten", SqlDbType.NVarChar, voteAnswer.VoteAnswerConten);
			sql.AddParameter("@VoteItemID", SqlDbType.Int, voteAnswer.VoteItemID);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteAnswer);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteAnswer.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteAnswer voteAnswer)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteAnswerConten", SqlDbType.NVarChar, voteAnswer.VoteAnswerConten);
			sql.AddParameter("@VoteItemID", SqlDbType.Int, voteAnswer.VoteItemID);
			sql.AddParameter("@Id", SqlDbType.Int, voteAnswer.Id);
			sql.ExecuteSql(SqlUpdateVoteAnswer);
		}

		public void Delete(Core.Business.VoteAnswer voteAnswer)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteAnswer.Id);
			sql.ExecuteSql(SqlDeleteVoteAnswer);
		}

		public IList<Core.Business.VoteAnswer> GetAllVoteAnswer()
		{
			IList<Core.Business.VoteAnswer> voteAnswerlist = new List<Core.Business.VoteAnswer>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteAnswer);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteAnswer voteAnswer = new Core.Business.VoteAnswer();

					if (!reader.IsDBNull(0)) voteAnswer.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteAnswer.VoteAnswerConten = reader.GetString(1);
					if (!reader.IsDBNull(2)) voteAnswer.VoteItemID = reader.GetInt32(2);

					voteAnswer.MarkOld();
					voteAnswerlist.Add(voteAnswer);
				}
				reader.Close();
			}
			return voteAnswerlist;
		}
		#endregion
	}
}
